<template>
  <div id="main-search" class="main-search">
    <el-form
      ref="itemList"
      :rules="rules"
      inline
      :label-width="labelWidth"
      :model="form_data"
    >
      <!-- <el-form-item label="常住地址"><usual-address /></el-form-item> -->
      <el-col
        v-for="(item, index) in itemList"
        :key="item.prop"
        :span="span !== 5 ? span : (index + 2) % 5 == 0 ? 4 : 5"
      >
        <el-form-item :label="item.label" class="item" :prop="item.prop">
          <!-- 常用地址 -->
          <el-cascader
            v-if="item.type === 'Cascader'"
            :style="{ width: '100%' }"
            v-model="form_data[item.prop]"
            :options="item.options"
            :props="{ checkStrictly: true }"
            clearable
            @change="item.change"
          />
          <!-- 输入框 -->
          <el-input
            v-if="item.type === 'Input'"
            v-model.trim="form_data[item.prop]"
            :style="{ width: '100%' }"
          />
          <!-- 下拉框 -->
          <el-select
            v-if="item.type === 'Select'"
            v-model.trim="form_data[item.prop]"
            :style="{ width: '100%' }"
            @change="item.change(form_data[item.prop])"
          >
            <el-option
              v-for="op in item.options"
              :key="op.value"
              :label="op.label"
              :value="op.value"
            />
          </el-select>
          <!-- 日期 -->
          <el-date-picker
            v-if="item.type === 'data'"
            v-model="form_data[item.prop]"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            :style="{ width: '100%' }"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-col>
    </el-form>
  </div>
</template>

<script>
import { eventBus } from "@/main.js"
// import UsualAddress from "@/components/UsualAddress/index.vue"
export default {
  name: "SearchMain",
  // components: { UsualAddress },
  props: {
    labelWidth: {
      type: String,
      default: "100px"
    },
    itemList: {
      type: Array,
      default: []
    },
    model: {
      type: Object,
      default: () => {}
    },
    rules: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      span: 6,
      // 获取form数据
      form_data: {}
    }
  },
  created() {
    // 监听点击搜索按钮
    eventBus.$on("toMainSearch", formName => {
      console.log(formName)
      this.search(formName)
    })
    // 监听重置按钮事件
    eventBus.$on("toMainSearchReset", message => {
      this.reset()
    })
  },
  mounted() {},
  methods: {
    // 点击按钮
    handle() {
      console.log("点击了")
    },
    // 监听传过来的点击事件 搜索点击
    search(formName) {
      const search = {}
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 过滤空字段
          for (let key in this.form_data) {
            if (this.form_data[key]) {
              search[key] = this.form_data[key]
            }
          }
          console.log(search)
          // 调用接口
          this.$emit("callbackComponent", {
            function: "search",
            data: search
          })
        } else {
          console.log("error submit!!")
          return false
        }
      })
    },
    // 重置
    reset() {
      this.$refs.itemList.resetFields()
      // 常用地址
      if (this.form_data.Address_value) {
        this.form_data.Address_value = []
      }
    }
  },
  watch: {
    model: {
      handler(newValue) {
        console.log(newValue)
        this.form_data = newValue
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
.main-search {
  overflow: hidden;
  padding-top: 15px;
  .item {
    width: 100%;
    display: flex;
    /deep/.el-form-item__content {
      flex: 1;
    }
  }
  .marginLeft {
    margin-left: 20px;
  }
  /deep/ .el-form-item--mini .el-form-item__error {
    padding-top: 3px;
  }
}
</style>
